function A = plotRegionPeakTiming(datatable, header, cfg)
%PLOTPEAKTIMING
if ~exist('cfg', 'var')
    cfg = plotPeakTiming_defaultCfg();
end

%% Filter the data
tfSelect = true(size(datatable,1),1);
% Select the significant peaks
if ~isnan(cfg.PValueLessThanSelection)
    tfSelect = tfSelect & ([datatable{:,strcmp(header,'P')}]<cfg.PValueLessThanSelection)';
end
% Select the Region of Interest
if ~isempty(cfg.ChannelSelection)
    tfSelect = tfSelect & strcmp(datatable(:,strcmp(header,'Channel')), cfg.ChannelSelection);
end
% SelecSt the frequency bin
if ~isempty(cfg.BinSelection)
    tfSelect = tfSelect & strcmp(datatable(:,strcmp(header,'Bin')), cfg.BinSelection);
end
% Select the subject
if ~isempty(cfg.SubjectSelection) 
	tfSelect = tfSelect & strcmp(datatable(:,strcmp(header,'Subject')), cfg.SubjectSelection);
end
% Execute the filter
datatable = datatable(tfSelect,:);


%% Generate the viewdata for the plot
viewItems = unique(datatable(:,strcmp(header, cfg.View)));
DRAWDATA = [];
for r = 1:length(viewItems)
    viewItem = viewItems{r};
    subset = datatable(strcmp(datatable(:,strcmp(header, cfg.View)), viewItem),:);
    for s = 1:size(subset,1)
        DRAWDATA(end+1).viewItemNo = r;
        DRAWDATA(end).x1 = subset{s,strcmp(header,'F30p Latency')};
        DRAWDATA(end).x2 = subset{s,strcmp(header,'Latency (ms)')};
        DRAWDATA(end).type = subset{s, strcmp(header,'Type')};
        DRAWDATA(end).viewItem = viewItem;
        DRAWDATA(end).vCfg = GetViewItemConfig(cfg, viewItem);
    end    
end 

% Get the current axes
A = gca;

% Prepare the plot layout
set(gca, ...
    'YTickLabel', {}, ...
    'YTick', []);
ylim([0 18]);
xlim([-2200 2200]);
for prI = 1:length(cfg.PlotRegions)
    PR = cfg.PlotRegions(prI);
    P = patch( ...
        [-2200 -2200 2200 2200], [PR.From PR.To PR.To PR.From], ...
        PR.BackgroundColor, 'EdgeAlpha', 0, 'Parent', A);  % Square
end

% Draw the gridlines
for s = 1:length(DRAWDATA);
    x1 = DRAWDATA(s).x1;
    y = DRAWDATA(s).vCfg.YLocation;
    L = line([x1 x1],[y 0], 'Color', [0.7 0.7 0.7], 'LineWidth', 0.5, 'LineStyle', '--', 'Parent', A);
    set(get(get(L,'Annotation'),'LegendInformation'), 'IconDisplayStyle', 'off');
end

% Draw the timing blocks
for s = 1:length(DRAWDATA);
    x1 = DRAWDATA(s).x1; x2 = DRAWDATA(s).x2;
    viewItem = DRAWDATA(s).viewItem;
    t = DRAWDATA(s).type;
%     p.FaceVertexCData = GetColor(coloring, channel)
    vCfg = DRAWDATA(s).vCfg;
    y = vCfg.YLocation; 
    P = patch([x1 x2 x2], [y-0.4 y-0.4 y+0.4], vCfg.Color, 'EdgeAlpha', 0.2, 'Parent', A);  % Triangle
%     P = patch([x1 x1 x2 x2], [y-0.4 y+0.4 y+0.4 y-0.4], vCfg.Color, 'EdgeAlpha', 0.2, 'Parent', A);  % Square
    text(x1-25, y, [vCfg.Label ' ' t], 'Interpreter', 'none', ...
        'FontSize', 5, 'FontAngle', 'italic', 'FontWeight', 'bold', ...
        'HorizontalAlignment', 'right', 'Parent', A); % 'BackgroundColor', [.7 .9 .7],
    set(P, 'DisplayName', vCfg.Label);
end

end

function VCfg = GetViewItemConfig(cfg, viewItem)
selectTf = strcmp({cfg.ViewItemConfig.Name}, viewItem);
if ~any(selectTf)
    selectTf = strcmp({cfg.ViewItemConfig.Name}, 'n/a');
end
VCfg = cfg.ViewItemConfig(find(selectTf,1,'first'));
end